import cv2
image=cv2.imread("hat.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow('Input',image)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
image1=cv2.erode(image,kernel)
image2=cv2.dilate(image1,kernel)
image3=cv2.dilate(image2,kernel)
image4=cv2.erode(image3,kernel)
cv2.imshow("Output",image4)
cv2.waitKey()
cv2.destroyAllWindows()